<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>01-组件传值-通过发布者订阅者</title>
    <script src="../libs/vue.min.js"></script>
    <script src="../libs/pubsub.js"></script>
</head>

<body>
    <div id="app">
        <h1>{{title}}</h1>
        <h3>{{msgFromSon}}</h3>
        <hr/>
        <son></son>
    </div>
</body>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            title: 'vue组件传值-通过发布者订阅者',
            msgFromSon:''
        },
        created(){
            pubsub.subscribe('msg',(topic,args)=>{
                this.msgFromSon=args;
                console.log(`来自子组件的消息:${args}`);
            });
        },
        components: {
            son: {
                data(){
                    return {
                        title:'子组件son',
                        msg:'a msg will send to parant'
                    }
                },
                template: `
                <div>
                    <h1>{{title}}</h1>
                    <input v-model="msg" />
                    <button @click="sendMsg">send</button>
                </div>
                `,
                methods:{
                    sendMsg(){
                        pubsub.publish('msg',this.msg);
                    }
                }
            }
        }
    });
</script>

</html>